package com.lechuang.youleduo1.presenter.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.MemoryCategory;
import com.lechuang.youleduo1.R;
import com.lechuang.youleduo1.model.bean.HomeSearchResultBean;
import com.lechuang.youleduo1.view.defineView.SpannelTextView;
import com.lechuang.youleduo1.view.defineView.SquareImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: guoning
 * Date: 2017/10/31
 * Description:
 */

public class HomeRecyclerAdapter extends RecyclerView.Adapter<HomeRecyclerAdapter.MyViewHolder> {

    private List<HomeSearchResultBean.ProductListBean> data;
    private Context mContext;
    private int isAgencyStatus;

    public HomeRecyclerAdapter(List<HomeSearchResultBean.ProductListBean> productList, Context context, int code) {
        data = productList;
        mContext = context;
        isAgencyStatus = code;
    }

    @Override
    public HomeRecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_program_product, parent, false);
        MyViewHolder vh = new MyViewHolder(view);
        return vh;
    }

    private int headImg;

    @Override
    public void onBindViewHolder(HomeRecyclerAdapter.MyViewHolder holder, int position) {
        HomeSearchResultBean.ProductListBean bean = data.get(position);
        //商品图
        Glide.with(mContext).load(bean.imgs).into(holder.ivImg);
        //动态调整滑动时的内存占用
        Glide.get(mContext).setMemoryCategory(MemoryCategory.LOW);
        holder.tvOldprice.setText("原价 ¥" + bean.price);
//        holder.tvName.setText(bean.name);
        holder.tvNowprice.setText("券后价 ¥" + bean.preferentialPrice);

        if (bean.shopType != null && bean.shopType.equals("2")) {
            headImg = R.drawable.zhuan_tianmao;
        } else {  //shopType 1淘宝 2天猫 默认淘宝
            headImg = R.drawable.zhuan_taobao;
        }
//        Drawable drawable = mContext.getResources().getDrawable(headImg);
//        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//        holder.tvName.setCompoundDrawables(drawable, null, null, null);
        holder.tvName.setShopType(bean.shopType == null ? 1 : Integer.parseInt(bean.shopType));
        holder.tvName.setDrawText(bean.name);

        holder.llLayout.setTag(position);
        holder.llLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickLitener != null) {
                    //注意这里使用getTag方法获取数据
                    mOnItemClickLitener.onItemClick(v, (int) v.getTag());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public interface OnItemClickLitener {
        void onItemClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.ll_layout)
        LinearLayout llLayout;
        @BindView(R.id.iv_img)
        SquareImageView ivImg;
        @BindView(R.id.tv_oldprice)
        TextView tvOldprice;
        @BindView(R.id.tv_nowprice)
        TextView tvNowprice;
        @BindView(R.id.tv_name)
        SpannelTextView tvName;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
